package com.jh.paymentchannel.pojo;

import cn.jh.common.core.base.Joiners;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.jh.paymentchannel.payment.alipay.AlipayConfig;

import java.io.Serializable;

import static io.jsonwebtoken.lang.Strings.hasText;

/**
 * @author Yichuan
 * @version 1.0
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AlipayAuthTokenVO implements Serializable {

    private static final long serialVersionUID = 8781763435910569174L;

    public static AlipayAuthTokenVO transform(String shopId, AlipayConfig alipayConfig,
                                              AlipayAuthToken authToken, String outPath) {
        AlipayAuthTokenVO vo = new AlipayAuthTokenVO();
        vo.setShopId(shopId);
        vo.setAppId(alipayConfig.getAppId());
        vo.setAlipayPubKey(alipayConfig.getAlipayPubKey());
        if (authToken == null || !authToken.isEnabled()) {
            vo.setStatus(AuthStatus.NONE.getValue());
        } else {
            if (authToken.checkAuthToken()) {
                vo.setOpenAppId(authToken.getAppId());
                vo.setOpenPid(authToken.getPid());
                vo.setAppAuthToken(authToken.getAppAuthToken());
                vo.setStatus(AuthStatus.AUTH.getValue());
            } else {
                vo.setStatus(AuthStatus.EXPIRE.getValue());
            }
        }
        if (authToken != null && hasText(authToken.getQrcCode())) {
            vo.setCodePath(Joiners.empty().join(outPath, authToken.getQrcCode()));
        }
        return vo;
    }

    public static AlipayAuthTokenVO onlyCode(AlipayAuthToken authToken, String outPath) {
        if (authToken == null || authToken.checkAuthToken() || !hasText(authToken.getQrcCode()))
            return null;

        return new AlipayAuthTokenVO(Joiners.empty().join(outPath, authToken.getQrcCode()));
    }

    public enum AuthStatus {
        NONE(0, "未授权"),
        AUTH(1, "已授权"),
        EXPIRE(2, "已过期")
        ;

        final short value;
        final String desc;

        AuthStatus(int value, String desc) {
            this.value = (short) value;
            this.desc = desc;
        }

        public short getValue() {
            return value;
        }

        public String getDesc() {
            return desc;
        }
    }

    // 商户id
    private String shopId;
    // 支付宝应用id
    private String appId;
    // 支付宝公钥
    private String alipayPubKey;
    // 支付宝授权商户appId
    private String openAppId;
    // 支付宝授权商户pid
    private String openPid;
    // 支付宝授权token
    private String appAuthToken;
    // 状态
    private Short status;
    // 授权二维码
    private String codePath;

    public AlipayAuthTokenVO() {
        super();
    }

    public AlipayAuthTokenVO(String codePath) {
        this.codePath = codePath;
    }

    public String getShopId() {
        return shopId;
    }

    public void setShopId(String shopId) {
        this.shopId = shopId;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAlipayPubKey() {
        return alipayPubKey;
    }

    public void setAlipayPubKey(String alipayPubKey) {
        this.alipayPubKey = alipayPubKey;
    }

    public String getOpenAppId() {
        return openAppId;
    }

    public void setOpenAppId(String openAppId) {
        this.openAppId = openAppId;
    }

    public String getOpenPid() {
        return openPid;
    }

    public void setOpenPid(String openPid) {
        this.openPid = openPid;
    }

    public String getAppAuthToken() {
        return appAuthToken;
    }

    public void setAppAuthToken(String appAuthToken) {
        this.appAuthToken = appAuthToken;
    }

    public Short getStatus() {
        return status;
    }

    public void setStatus(Short status) {
        this.status = status;
    }

    public String getCodePath() {
        return codePath;
    }

    public void setCodePath(String codePath) {
        this.codePath = codePath;
    }
}
